home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3816 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  51 lines

  1. Path: dialup-86.austin.io.com!user
  2. From: hamilton@shokwave.com (Jim Hamilton)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: Wed, 24 Jan 1996 01:04:02 -0600
  6. Organization: Disorganized
  7. Message-ID: <hamilton-2401960104020001@dialup-86.austin.io.com>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net>
  9. NNTP-Posting-Host: dialup-86.austin.io.com
  10. X-Newsreader: Yet Another NewsWatcher 2.0.5b5
  11.  
  12. In article <4dpcfo$293@clarknet.clark.net>, gusty@clark.net (Harlan
  13. Messinger) wrote:
  14.  
  15. >Jim Hamilton (hamilton@shokwave.com) wrote:
  16. >: In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
  17. >: <100336.3326@CompuServe.COM> wrote:
  18. >: 
  19. >: >Consider
  20. >: >#include <iostream>
  21. >: >int main() {
  22. >: >  int i=1;int j=i+(i+=1);
  23. >: >  cout<<i<<','<<j<<'\n';
  24. >: >  return 0;
  25. >: >}
  26. >: >on my compiler this produces 2,4
  27. [deleted]
  28.  
  29. >: The highest precedence in any expression is the insides of parentheses
  30. >: ().  Therefore (i+=1) is evaluated before i+().
  31. >
  32. >True, but that's not actually the problem here. The problem is whether 
  33. >(i+=1) gets evaluated before the i to the left of the plus sign.
  34.  
  35. And I said, what's inside the parentheses (i+=1) *does* gets evaluated
  36. before the i to the left of the plus sign.
  37.  
  38. Anyway, according to K&R, order of evaluation of operands of a commutative
  39. operator is undefined by the language.  If it were legal to write:
  40.    j = i + i+=1;   // spacing for readability of what's intended
  41. then the value of j would be undefined (either 3 or 4).
  42.  
  43. Of course, that statement is not legal, as operator precedence makes it
  44. look more like:
  45.    j = (i + i) += 1;    // error:  (i + i) is not an lvalue
  46.  
  47. -- 
  48. JFH
  49.  
  50. [This .sig intentionally left blank.]
  51.